package com.controller;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.beans.ShopInfo;
import com.service.ShopService;

@Controller  @RequestMapping("/shop")
public class ShopControl {
	@Resource
	private ShopService shopService;
	
	//用于国际化
	@Resource 
	private MessageSource messageSource;
		
	//全局的,用于转向某个页面
	@GetMapping("/{page}")
	public String toPage(@PathVariable("page") String page) {
		return page;   //默认会转向 src/main/resources/templates/+page+.html
	}
	
	//添加商品
	@PostMapping("/add")
	public String add(ShopInfo shop,ModelMap m)
	{
		m.put("shop",shop);
		shopService.add_shop(shop);
		m.put("msg","添加成功");
		return "shop-add";
	}
	
	@GetMapping("/getAll")
	public String getAll(ModelMap m) {
		List<ShopInfo> shopList= shopService.select_all();
		m.put("shopList",shopList);
		m.put("size",shopList.size());
		return "shop-manager";	
	}
	
	@GetMapping("/update")
	public String searchForUpdate(int flowerNo,ModelMap m) {
		ShopInfo shop=shopService.get_shop(flowerNo);
		m.put("shop", shop);
		return "shop-update"; 
	}
	
	@PostMapping("/update")
	public String update(ShopInfo shop,ModelMap m) {
		m.put("shop",shop);
		shopService.update_shop(shop);
		m.put("msg","商品信息更新成功");
		return "shop-update"; 
	}
	
	@GetMapping("/delete")
	public String delete(int flowerNo) {
		shopService.del_shop(flowerNo);
		return "forward:/shop/getAll";	
	}
}
